import config from './config' // 引入服务器相关配置
import { setCookie, getCookie, getToken } from './localCache'
export default (
  url,
  data = {},
  method = "GET"
) => new Promise((resolve, reject) => {
  wx.request({
    url: `${config.host}${url}`,
    data,
    method,
    header: {
      cookie: getCookie().toString() ? getCookie().toString() : '',
      "authorization": `Bearer ${getToken()}`
    },
    success: (res) => {
      // 判断是否有此自定义字段，如果有把cookie字段存入本地
      if (data.isCookie) {
        setCookie(res.cookies)
      }
      resolve(res.data) // 将成功的结果返回出去
    },
    fail: (err) => {
      reject(err) // 将失败的结果返回出去
    }
  })
})